package org.loofer.ext.utils;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Build.VERSION;
import cn.finalteam.toolsfinal.io.IOUtils;
import org.loofer.ext.component.logger.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class NativeLibraryUtils {
    private static final String APK_LIB = "lib/";
    private static final int APK_LIB_LEN = APK_LIB.length();
    private static final String LIB_PREFIX = "/lib";
    private static final int LIB_PREFIX_LEN = LIB_PREFIX.length();
    private static final String LIB_SUFFIX = ".so";
    private static final int LIB_SUFFIX_LEN = LIB_SUFFIX.length();
    private static final String TAG = "NativeLibraryUtils";

    private interface IterateHandler {
        boolean handleEntry(InputStream inputStream, ZipEntry zipEntry, String str);
    }

    private NativeLibraryUtils() {
    }

    public static boolean copyNativeBinariesIfNeeded(File apkFile, File sharedLibraryDir) {
        return copyNativeBinariesIfNeeded(apkFile.getPath(), sharedLibraryDir.getPath());
    }

    @SuppressLint({"InlinedApi"})
    public static boolean copyNativeBinariesIfNeeded(String apkPath, String sharedLibraryDir) {
        return copyNativeBinariesIfNeeded(apkPath, sharedLibraryDir, Build.CPU_ABI, VERSION.SDK_INT >= 8 ? Build.CPU_ABI2 : null, PropertyUtils.getQuickly("ro.product.cpu.upgradeabi", "armeabi"));
    }

    private static boolean copyNativeBinariesIfNeeded(String filePath, String sharedLibraryPath, String cpuAbi, String cpuAbi2, String cpuAbi3) {
        final String dstDir = sharedLibraryPath;
        return iterateOverNativeBinaries(filePath, cpuAbi, cpuAbi2, cpuAbi3, new IterateHandler() {
            public boolean handleEntry(InputStream is, ZipEntry ze, String name) {
                return NativeLibraryUtils.copyFileIfChanged(is, ze, dstDir, name);
            }
        });
    }

    public static boolean removeNativeBinaries(String nativeLibraryPath) {
        return removeNativeBinaries(new File(nativeLibraryPath));
    }

    public static boolean removeNativeBinaries(File nativeLibraryDir) {
        boolean deletedFiles = false;
        if (nativeLibraryDir.exists()) {
            File[] binaries = nativeLibraryDir.listFiles();
            if (binaries != null) {
                for (int nn = 0; nn < binaries.length; nn++) {
                    if (binaries[nn].delete()) {
                        deletedFiles = true;
                    } else {
                        Logger.w(TAG, "Could not delete native binary: " + binaries[nn].getPath());
                    }
                }
            }
        }
        return deletedFiles;
    }

    private static boolean iterateOverNativeBinaries(String filePath, String cpuAbi, String cpuAbi2, String cpuAbi3, IterateHandler handler) {
        Throwable th;
        ZipFile zipFile = null;
        try {
            ZipFile zf = new ZipFile(filePath);
            boolean hasPrimaryAbi = false;
            boolean hasSecondaryAbi = false;
            InputStream is;
            try {
                Enumeration<? extends ZipEntry> entries = zf.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry ze = (ZipEntry) entries.nextElement();
                    String fileName = ze.getName();
                    if (fileName != null && fileName.startsWith(APK_LIB) && fileName.length() >= (((APK_LIB_LEN + 2) + LIB_PREFIX_LEN) + 1) + LIB_SUFFIX_LEN) {
                        String nameWithSlash = fileName.substring(fileName.lastIndexOf(47));
                        if (nameWithSlash.endsWith(LIB_SUFFIX) && nameWithSlash.startsWith(LIB_PREFIX)) {
                            if (fileName.regionMatches(APK_LIB_LEN, cpuAbi, 0, cpuAbi.length()) && fileName.charAt(APK_LIB_LEN + cpuAbi.length()) == IOUtils.DIR_SEPARATOR_UNIX) {
                                hasPrimaryAbi = true;
                            } else {
                                if (cpuAbi2 != null) {
                                    if (fileName.regionMatches(APK_LIB_LEN, cpuAbi2, 0, cpuAbi2.length()) && fileName.charAt(APK_LIB_LEN + cpuAbi2.length()) == IOUtils.DIR_SEPARATOR_UNIX) {
                                        hasSecondaryAbi = true;
                                        if (hasPrimaryAbi) {
                                            Logger.i(TAG, "Already saw primary ABI, skipping secondary ABI " + cpuAbi2);
                                        } else {
                                            Logger.i(TAG, "Using secondary ABI " + cpuAbi2);
                                        }
                                    }
                                }
                                if (cpuAbi3 != null) {
                                    if (fileName.regionMatches(APK_LIB_LEN, cpuAbi3, 0, cpuAbi3.length()) && fileName.charAt(APK_LIB_LEN + cpuAbi3.length()) == IOUtils.DIR_SEPARATOR_UNIX) {
                                        if (hasPrimaryAbi || hasSecondaryAbi) {
                                            Logger.i(TAG, "Already saw primary or secondary ABI, skipping third ABI " + cpuAbi3);
                                        } else {
                                            Logger.i(TAG, "Using third ABI " + cpuAbi3);
                                        }
                                    }
                                }
                                Logger.i(TAG, "abi didn't match anything entry " + fileName + ", ABI is " + cpuAbi + " ABI2 is " + cpuAbi2 + " ABI3 is " + cpuAbi3);
                            }
                            is = zf.getInputStream(ze);
                            if (!handler.handleEntry(is, ze, nameWithSlash.substring(1))) {
                                Logger.w(TAG, "Failure for handle match entry " + fileName);
                                if (is != null) {
                                    is.close();
                                }
                                if (zf != null) {
                                    try {
                                        zf.close();
                                    } catch (IOException e) {
                                    }
                                }
                                zipFile = zf;
                                return false;
                            } else if (is != null) {
                                is.close();
                            }
                        }
                    }
                }
                if (zf != null) {
                    try {
                        zf.close();
                    } catch (IOException e2) {
                    }
                }
                zipFile = zf;
                return true;
            } catch (IOException e3) {
                zipFile = zf;
            } catch (Throwable th2) {
                th = th2;
                zipFile = zf;
            }
        } catch (IOException e4) {
            try {
                Logger.w(TAG, "Couldn't open APK " + filePath);
                if (zipFile == null) {
                    return false;
                }
                try {
                    zipFile.close();
                    return false;
                } catch (IOException e5) {
                    return false;
                }
            } catch (Throwable th3) {
                th = th3;
                if (zipFile != null) {
                    try {
                        zipFile.close();
                    } catch (IOException e6) {
                    }
                }
                throw th;
            }
        }
    }

    private static boolean copyFileIfChanged(InputStream is, ZipEntry ze, String dstDir, String dstName) {
        IOException e;
        Throwable th;
        FileUtils.mkdirs(new File(dstDir));
        String dstPath = dstDir + File.separator + dstName;
        if (!isFileDifferent(dstPath, ze.getSize(), ze.getTime(), ze.getCrc())) {
            return true;
        }
        File dstFile = new File(dstPath);
        FileOutputStream fileOutputStream = null;
        try {
            FileOutputStream fos = new FileOutputStream(dstFile);
            try {
                byte[] buffer = new byte[1024];
                while (true) {
                    int numBytes = is.read(buffer);
                    if (numBytes <= 0) {
                        break;
                    }
                    fos.write(buffer, 0, numBytes);
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e2) {
                    }
                }
                if (!dstFile.setLastModified(ze.getTime())) {
                    Logger.w(TAG, "Couldn't set time for dst file " + dstFile);
                }
                return true;
            } catch (IOException e3) {
                e = e3;
                fileOutputStream = fos;
                try {
                    Logger.w(TAG, "Couldn't write dst file " + dstFile, e);
                    FileUtils.delete(dstFile);
                    if (fileOutputStream != null) {
                        return false;
                    }
                    try {
                        fileOutputStream.close();
                        return false;
                    } catch (IOException e4) {
                        return false;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e5) {
                        }
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = fos;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                throw th;
            }
        } catch (IOException e6) {
            e = e6;
            Logger.w(TAG, "Couldn't write dst file " + dstFile, e);
            FileUtils.delete(dstFile);
            if (fileOutputStream != null) {
                return false;
            }
            fileOutputStream.close();
            return false;
        }
    }

    private static boolean isFileDifferent(String filePath, long fileSize, long modifiedTime, long zipCrc) {
        FileNotFoundException e;
        Throwable th;
        IOException e2;
        File file = new File(filePath);
        if (file.length() != fileSize) {
            Logger.i(TAG, "file size doesn't match: " + file.length() + " vs " + fileSize);
            return true;
        } else if (file.lastModified() != modifiedTime) {
            Logger.i(TAG, "mod time doesn't match: " + file.lastModified() + " vs " + modifiedTime);
            return true;
        } else {
            FileInputStream fis = null;
            try {
                FileInputStream fis2 = new FileInputStream(file);
                try {
                    CRC32 crc32 = new CRC32();
                    byte[] crcBuffer = new byte[8192];
                    while (true) {
                        int numBytes = fis2.read(crcBuffer);
                        if (numBytes <= 0) {
                            break;
                        }
                        crc32.update(crcBuffer, 0, numBytes);
                    }
                    long crc = crc32.getValue();
                    Logger.i(TAG, filePath + ": crc = " + crc + ", zipCrc = " + zipCrc);
                    if (crc == zipCrc) {
                        if (fis2 != null) {
                            try {
                                fis2.close();
                            } catch (IOException e3) {
                            }
                        }
                        return false;
                    } else if (fis2 == null) {
                        return true;
                    } else {
                        try {
                            fis2.close();
                            return true;
                        } catch (IOException e4) {
                            return true;
                        }
                    }
                } catch (FileNotFoundException e5) {
                    e = e5;
                    fis = fis2;
                    try {
                        Logger.w(TAG, "Couldn't open file " + filePath, e);
                        if (fis != null) {
                            return true;
                        }
                        try {
                            fis.close();
                            return true;
                        } catch (IOException e6) {
                            return true;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        if (fis != null) {
                            try {
                                fis.close();
                            } catch (IOException e7) {
                            }
                        }
                        throw th;
                    }
                } catch (IOException e8) {
                    e2 = e8;
                    fis = fis2;
                    Logger.w(TAG, "Couldn't read file " + filePath, e2);
                    if (fis != null) {
                        return true;
                    }
                    try {
                        fis.close();
                        return true;
                    } catch (IOException e9) {
                        return true;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    fis = fis2;
                    if (fis != null) {
                        fis.close();
                    }
                    throw th;
                }
            } catch (FileNotFoundException e10) {
                e = e10;
                Logger.w(TAG, "Couldn't open file " + filePath, e);
                if (fis != null) {
                    return true;
                }
                fis.close();
                return true;
            } catch (IOException e11) {
                e2 = e11;
                Logger.w(TAG, "Couldn't read file " + filePath, e2);
                if (fis != null) {
                    return true;
                }
                fis.close();
                return true;
            }
        }
    }
}
